<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/1/31
 * Time: 10:45
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Goodspackaging extends Base
{
    /**
     * 产品包装首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $packaging_status = input('packaging_status');
            $packaging_name = input('packaging_name');
            $where['packaging_status'] = ['BETWEEN', [1, 2]];

            if($packaging_status) $where['packaging_status'] = $packaging_status;
            if($packaging_name) $where['packaging_name'] = ['LIKE', '%' . $packaging_name . '%'];

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods_packaging')->where($where)->count();// 获取总条数
            $lists = Db::table('up_goods_packaging')
                ->where($where)
                ->field('packaging_id, packaging_name, packaging_status, packaging_time, packaging_sort')
                ->page($Nowpage, $limits)
                ->order('packaging_sort desc, packaging_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 添加包装
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function packagingAdd(){
        if(Request::instance()->isPost()){
            $packaging_name = input('packaging_name');
            $packaging_status = input('packaging_status');
            $packaging_sort = input('packaging_sort');

            $has = Db::table('up_goods_packaging')->where(['packaging_name' => $packaging_name])->find();
            if($has) return json(['code' => 100, 'msg' => '该包装已存在']);

            $insert = [
                'packaging_name' => $packaging_name,
                'packaging_status' => $packaging_status,
                'packaging_sort' => $packaging_sort,
                'packaging_time' => initTime()
            ];
            $res = Db::table('up_goods_packaging')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑包装
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function packagingEdit(){
        $packaging_id = input('packaging_id');
        if(Request::instance()->isPost()){
            $packaging_name = input('packaging_name');
            $packaging_status = input('packaging_status');
            $packaging_sort = input('packaging_sort');

            $has = Db::table('up_goods_packaging')
                ->where(['packaging_name' => $packaging_name])
                ->find();
            if($has && ($packaging_id != $has['packaging_id'])) return json(['code' => 100, 'msg' => '该包装已存在']);

            $update = [
                'packaging_name' => $packaging_name,
                'packaging_status' => $packaging_status,
                'packaging_sort' => $packaging_sort,
                'packaging_update_time' => initTime()
            ];
            $res = Db::table('up_goods_packaging')->where('packaging_id', $packaging_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $data = Db::table('up_goods_packaging')->where('packaging_id', $packaging_id)->field('packaging_id, packaging_name, packaging_status, packaging_sort')->find();
        return $this->fetch('', ['data' => $data]);
    }

    /**
     * 上架/下架
     * @return \think\response\Json
     */
    public function goodsPackagingChangeStatus(){
        $packaging_id = input('id');
        $status = Db::table('up_goods_packaging')->where('packaging_id', $packaging_id)->value('packaging_status');
        $newStatus = $status == 1 ? 2 : 1;
        $res = Db::table('up_goods_packaging')->where('packaging_id', $packaging_id)->setField('packaging_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function goodsPackagingDel(){
        $packaging_id = input('id');
        $res = Db::table('up_goods_packaging')->where('packaging_id', $packaging_id)->setField('packaging_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}